﻿package fui.FUIControllers{
	import fl.containers.ScrollPane;
	import fui.FUIControllers.*;
	import fui.FUIData.*;
	public class FUIComponentsController{
		private var _cName:Array;
		private var _cHandlers:Array;
		private var _authFlag:Boolean;
		private var _uisess:UISession;
		private var mainP:ScrollPane;
		
		
		public function FUIComponentsController(uis:UISession){
			_cName = new Array();
			_cHandlers = new Array();
			_uisess = uis;
			
		}
		public function setRef2MainPane(mp:ScrollPane):void
		{
			mainP = mp;
		}
		public function set authFlag(flag:Boolean):void{
			this._authFlag = flag;
		}
		public function registerNewComponent(component:IComponent)
		{
			trace("Registration of " + component.get_component_title() + ", Component: " + component);
			component.set_session(_uisess);
			if(component.if_need_auth() == false)
			{
				_cName.push(component.get_component_title());
				trace("before hide component");
				
			}
			component.hide_component();
			_cHandlers.push(component);
			trace("added componentn: " + component.get_component_title() + " into cHandlers array");
			
			
		}
		public function rebuildCNames():void
		{
			//Bullshit, shoud change logic
			while(_cName.length>0)
			{
				_cName.pop();
			}
			for (var ind = 0; ind < _cHandlers.length; ind++)
			{
				trace(_cHandlers[ind].get_component_title());
				if (_uisess.isLoggedIn() == true)
				{
					_cName.push(_cHandlers[ind].get_component_title());
				}
				else
				{
					if(_cHandlers[ind].if_need_auth() == false )//&& _uisess._logged == true)
					{
						_cName.push(_cHandlers[ind].get_component_title());
					}
				}
			}
		}
		public function upComponent(ctitle:String):void
		{
			trace("Title: " + ctitle);
			for (var it = 0;it < _cHandlers.length; it++)
			{
				if ( _cHandlers[it].get_component_title() == ctitle)
				{
					trace("show component");
					_cHandlers[it].show_component();
				}
				else
				{
					_cHandlers[it].hide_component();
				}
			}
			
		}
		public function get cNames():Array
		{
			return this._cName;
		}
		
	}
}